﻿using System.Text.Json;
using Autofac.Annotation;
using Microsoft.AspNetCore.Mvc;
using TS.Model;
using TS.Services;

namespace TaskScheduling.Controllers;

[Component]
[Route("/[controller]/[action]")]
public class HomeController : Controller
{
    [Autowired] public IConfiguration configuration { get; set; }

    [Autowired] public AuthorizationHandlerSerivce authorizationHandlerSerivce { get; set; }

    [HttpPost]
    public IActionResult Login([FromBody]AuthenUser user)
    {
        if (user.password.Substring(1, 30) == configuration["admin_token"])
        {
            user.name = "admin";
            user.password = "";
            var token = authorizationHandlerSerivce.GenerateTokenCredential(user.name, JsonSerializer.Serialize(user));
            return Json(new HttpResult
            {
                data = token
            });
        }
        return Json(new HttpResult
        {
            code = 1,
            msg = "密码不正确"
        });
    }
}